﻿using System;
using System.Windows;
using System.Windows.Input;

namespace Gmantis.Controls
{
    public static class PromptBox
    {
        // Methods
        public static void Show(string message, string caption, Action<string, MessageBoxResult> callback)
        {
            Show(message, caption, null, callback);
        }

        public static void Show(string message, string caption, string defaultValue, Action<string, MessageBoxResult> callback)
        {
            EventHandler handler = null;
            PromptBoxContent content = new PromptBoxContent
            {
                Message = message
            };
            if (ContentStyle != null)
            {
                content.Style = ContentStyle;
            }
            if (!string.IsNullOrEmpty(defaultValue))
            {
                content._text = defaultValue;
            }
            GWindow window = new GWindow
            {
                Header = caption,
                Content = content,
                IsResizable = false,
                ShowMinimizeButton = false
            };
            if (WindowStyle != null)
            {
                window.Style = WindowStyle;
            }
            window.CenterOnScreen();
            window.ShowModal();
            MessageBoxResult result = MessageBoxResult.Cancel;
            if (callback != null)
            {
                if (handler == null)
                {
                    handler = (s, e) => callback(content.Text, result);
                }
                window.Closed += handler;
            }
            content.ButtonClick += delegate(object s, EventArgs e)
            {
                result = MessageBoxResult.OK;
                window.Close();
            };
            content.KeyDown += delegate(object s, System.Windows.Input.KeyEventArgs e)
            {
                if (e.Key == Key.Enter)
                {
                    result = MessageBoxResult.OK;
                    window.Close();
                }
                if (e.Key == Key.Escape)
                {
                    window.Close();
                }
            };
        }

        public static Style ContentStyle { get; set; }
        public static Style WindowStyle { get; set; }
    }
}
